package models

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

type Shop struct {
	Id     int     `gorm:"type:int" json:"id"`
	Name   string  `gorm:"type:varchar(30)" json:"name"`
	Price  float64 `gorm:"type:decimal(10,2)" json:"price"`
	Type   string  `gorm:"type:varchar(30)" json:"type"`
	Img    string  `gorm:"type:varchar(100)" json:"img"`
	Status string  `gorm:"type:varchar(30)" json:"status"`
}

var DB *gorm.DB

func Init() {
	dsn := "root:root@tcp(127.0.0.1:3306)/shop"
	var err error
	DB, err = gorm.Open(mysql.Open(dsn))
	if err != nil {
		println("数据库连接失败")
		return
	}
	fmt.Println("数据库连接成功")
	err = DB.AutoMigrate(&Shop{})
	if err != nil {
		fmt.Println("迁移失败")
		return
	} else {
		fmt.Println("迁移成功")
	}
}
